package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.CouponsListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.MyCouponBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IMyCouponListView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class MyCouponListPresenter
{
    private MyCouponBean myCouponBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IMyCouponListView iMyCouponListView;
    CouponsListAdapter couponsListAdapter;
    private Context context;
    private BaseView baseView;
    public MyCouponListPresenter(Context context, IMyCouponListView iMyCouponListView, BaseView baseView)
    {
        this.context = context;
        this.iMyCouponListView = iMyCouponListView;
        this.baseView=baseView;
    }

    /**
     * 获取我的优惠券列表
     */
    public void getMyConponList()
    {
        baseView.showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getMyCoupon(UrlData.BASEURL + UrlData.MYCOUPON, SharePrefencesUtil.getUser_id(context),iMyCouponListView.getmyPage() + "", "0", handler);
    }
    public MyCouponBean getData(){

        return myCouponBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            iMyCouponListView.onLoad();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);
                        baseView.hideLoading();
                        myCouponBean= gson.fromJson(result, MyCouponBean.class);
                        couponsListAdapter=new CouponsListAdapter(context,myCouponBean);
                        if(myCouponBean.getData().size()==0&iMyCouponListView.getmyPage()==1){
                            iMyCouponListView.getnoContent().setVisibility(View.VISIBLE);
                            iMyCouponListView.getListView().setVisibility(View.GONE);
                        }else {
                            iMyCouponListView.getnoContent().setVisibility(View.GONE);
                            iMyCouponListView.getListView().setVisibility(View.VISIBLE);
                            if(iMyCouponListView.getmyPage()==1)
                            {
                                iMyCouponListView.getListView().setAdapter(couponsListAdapter);
                            }else
                            {
                                couponsListAdapter.addData(myCouponBean);
                            }
                            if(myCouponBean.getTotal()<=(myCouponBean.getEach()*iMyCouponListView.getmyPage()))
                            {
                                iMyCouponListView.getListView().setPullLoadEnable(false);
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.hideLoading();
                        baseView.showError(result);
                        iMyCouponListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}